﻿#ifndef _EASY_SELECT_SERVER_H_
#define _EASY_SELECT_SERVER_H_

#include "EasyTcpServer.hpp"
#include "CELLTimer.hpp"
#include "CELLSelectServer.hpp"

class EasySelectServer : public EasyTcpServer
{
public:
	void Start(int nCELLServer)
	{
		EasyTcpServer::Start<CELLSelectServer>(nCELLServer);
	}
private:
	// 处理网络消息
	virtual void OnRun(CELLThread* pthread) override
	{
		long second;
		long microseconds;
		while (pthread->isRun())
		{
			time4msg();
			fd_set fdRead;
			FD_ZERO(&fdRead);

			FD_SET(sockfd(), &fdRead);
			time_t time = _time_events.getTime();

			second = time / 1000;
			microseconds = time % 1000 * 1000;
			struct timeval ti = { second, microseconds };
			int ret = select(sockfd() + 1, &fdRead, 0, 0, &ti);
			if (ret < 0)
			{
				if (errno == EINTR)
				{
					continue;
				}

				CELLLog_pError("EasyTcpServer OnRun select error");
				pthread->Exit();
				return;
			}
			if (FD_ISSET(sockfd(), &fdRead))
			{
				// 监听套接字有新连接
				Accept();
			}
			_time_events.process_timer();
		}
		CELLLog_Info("EasyTcpServer OnRun exit");
	}
};

#endif // #ifndef _EASY_SELECT_SERVER_H_
